<?php
/**
 * Mortar
 *
 * @copyright Copyright (c) 2009, Robert Hafner
 * @license http://www.mozilla.org/MPL/
 * @package System
 * @subpackage Dashboard
 */

/**
 * The ViewControlDisplay is used to render and display Controls from ControlSets for the Mortar Dashboard. It uses the
 * ViewThemeTemplate functionality, while wrapping the specific Control content in a pre-defined HTML wrapper and
 * adding several Dashboard-specific form controls.
 *
 * @package System
 * @subpackage Dashboard
 */
class ViewControlDisplay
{

	/**
	 * The ControlSet being rendered by this view.
	 *
	 * @access protected
	 * @var ControlSet
	 */
	protected $controlset;

	/**
	 * The Theme with which the ControlSet is currently being rendered.
	 *
	 * @access protected
	 * @var Theme
	 */
	protected $theme;

	/**
	 * The name of the template file which will be used to render the content.
	 *
	 * @access protected
	 * @var array
	 */
	protected $template = 'support/Control.html';

	/**
	 * Constructor sets the ControlSet and Theme for the class.
	 *
	 * @param ControlSet $controlset
	 * @param Theme $theme
	 */
	public function __construct(ControlSet $controlset, Theme $theme)
	{
		$this->controlset = $controlset;
		$this->theme = $theme;
	}

	/**
	 * Sets the name for the template to be used in rendering the Control content.
	 *
	 * @param string $template
	 */
	public function useTemplate($template)
	{
		$this->template = $template;
	}

	/**
	 * Returns the HTML content generated by rendering the ControlSet. For each Control, the template is
	 * rendered using the content and classes from the Control class, as well as the links from the
	 * getLinks method.
	 *
	 * @return string
	 */
	public function getDisplay()
	{
		$controls = $this->controlset->getControls();

		$controlContent = '';

		foreach($controls as $key => $control) {
			$classes = $control->getClasses();
			$content = $control->display();
			$links = $this->getLinks($key);

			$controlView = new ViewThemeTemplate($this->theme, $this->template);
			$controlView->addContent(array('content' => $content, 'classes' => $classes,
				'links' => $links));
			$controlContent .= $controlView->getDisplay();
		}

		return $controlContent;
	}

	/**
	 * Generates a series of form buttons which pass relevant data to the ControlModify action. Each Control
	 * receives the relevant subset of Settings, Move Up, Move Down, and Remove. This method generates
	 * form controls rather than links in order to follow REST best-practices -- permanent changes to state
	 * should be driven by POST events rather than GET. The form encodes the user ID of the requester and
	 * ID of the Control being acted upon so that requests are not mistakenly acted upon as a result of out-of-date
	 * requests.
	 *
	 * @param int $pos
	 * @return string
	 */
	protected function getLinks($pos)
	{
		$links = new HtmlObject('div');
		$links->addClass('dashboard_links');

		$info = $this->controlset->getInfo();
		$query = Query::getQuery();
		$link = new Url();
		$link->module = PackageInfo::loadByName(null, 'Mortar');
		$link->format = $query['format'];
		$link->action = 'ControlModify';
		$link->id = $pos;

		$basebutton = new HtmlObject('button');
		$basebutton->property('name', 'modify');
		$basebutton->property('type', 'Submit');

		$form = new HtmlObject('form');
		$form->property('action', (string) $link);
		$form->property('method', 'post');

		$item = new HtmlObject('input');
		$item->property('type', 'hidden');
		$item->property('name', 'user');
		$item->property('value', $this->controlset->getUserId());
		$form->wrapAround($item);

		$item = new HtmlObject('input');
		$item->property('type', 'hidden');
		$item->property('name', 'id');
		$item->property('value', $info[$pos]['id']);
		$form->wrapAround($item);

		$button = clone($basebutton);
		$button->property('value', 'Settings');
		$button->wrapAround('Settings');
		$form->wrapAround($button);

		if($pos != 0) {
			$button = clone($basebutton);
			$button->property('value', 'Move Up');
			$button->wrapAround('Move Up');
			$form->wrapAround($button);
		}

		if($pos != (count($info) - 1)) {
			$button = clone($basebutton);
			$button->property('value', 'Move Down');
			$button->wrapAround('Move Down');
			$form->wrapAround($button);	
		}

		$button = clone($basebutton);
		$button->property('value', 'Remove');
		$button->wrapAround('Remove');
		$form->wrapAround($button);	

		$links->wrapAround($form);
		return (string) $links;
	}
}

?>